package com.zcb.myrxjavathreecache.tools;

import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

public class PreferenceUtil {
    private static SharedPreferences setting;
    private static SharedPreferences.Editor pen;
    private final static String SHARE_NAME = "wzly_gdswww_user";
    public final static String USER_DATA = "user_data";
    public final static String LOGIN_ID = "user_id";
    public final static String AVATAR = "avatar";
    public final static String IMAGE = "image";

    /**
     * 根据键获取值JSON
     *
     * @param context
     * @param key
     * @return
     */
    public static String getString_Json_Value(Context context, String key,
                                              String key2) {
        // 只被本应用使用
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
//		try {
//			JSONObject json = new JSONObject(setting.getString(key, ""));
//			return json.getString(key2);
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			return "";
//		}
        return key.equals("id") ? "100048" : "b49679ea04f3a851d772aabdbd5c960e";
    }


    /**
     * @param context
     * @param key
     * @param value
     */
    public static void SetJsonKey_Vallue(Context context, String key,
                                         String value) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        try {
            JSONObject json = new JSONObject(setting.getString(USER_DATA, ""));
            json.put(key, value);
            setStringValue(context, USER_DATA, json.toString());
        } catch (JSONException e) {
        }
    }


    /**
     * @param context
     * @param key
     * @param value
     */
    public static void setStringValue(Context context, String key, String value) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        pen = setting.edit();
        pen.putString(key, value);
        pen.commit();
    }

    public static void clearAllPrefer() {
        if (pen != null) {
            pen.clear().commit();
        }
    }

    public static void clearAllPrefer(Context context) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        pen = setting.edit();
        if (pen != null) {
            pen.clear().commit();
        }
    }

    public static String getStringValue(Context context, String key) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        return setting.getString(key, "");
    }

    public static void setIntValue(Context context, String key, int value) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        pen = setting.edit();
        pen.putInt(key, value);
        pen.commit();
    }

    public static int getIntValue(Context context, String key) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        return setting.getInt(key, -1);
    }

    public static void setBooleanValue(Context context, String key,
                                       boolean value) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        pen = setting.edit();
        pen.putBoolean(key, value);
        pen.commit();
    }

    public static void clearJsonValue(Context context, String key, boolean value) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        pen = setting.edit();
        pen.clear();
        pen.commit();
    }

    public static boolean getBooleanValue(Context context, String key) {
        setting = context.getSharedPreferences(SHARE_NAME,
                Context.MODE_PRIVATE);
        return setting.getBoolean(key, false);
    }


    public static void setPrefersetbyPreferFileName(String fileName,
                                                    String key, String value, Context context) {
        SharedPreferences preferences = context.getSharedPreferences(fileName,
                Context.MODE_PRIVATE);
        preferences.edit().putString(key, value).commit();
    }
}
